#!/usr/bin/env node

console.log('🧪 测试 uni-app x + CoolUI 框架...\n')

const fs = require('fs')
const path = require('path')

// 检查关键文件是否存在
const requiredFiles = [
  'src/types/index.ts',
  'src/utils/storage.ts',
  'src/utils/request.ts',
  'src/api/index.ts',
  'src/store/index.ts',
  'src/config/index.ts',
  'src/components/BaseButton.uvue',
  'src/components/BaseInput.uvue',
  'src/pages/login/login.uvue',
  'src/pages/register/register.uvue',
  'pages/index/index.uvue',
  'App.uvue',
  'main.uts',
  'manifest.json',
  'pages.json',
  'package.json',
  'tsconfig.json',
  'vite.config.ts'
]

console.log('📁 检查关键文件:')
let allFilesExist = true

requiredFiles.forEach(file => {
  const filePath = path.join(process.cwd(), file)
  if (fs.existsSync(filePath)) {
    console.log(`  ✅ ${file}`)
  } else {
    console.log(`  ❌ ${file} - 文件不存在`)
    allFilesExist = false
  }
})

console.log('\n🔍 检查项目结构:')
const requiredDirs = [
  'src',
  'src/api',
  'src/components',
  'src/config',
  'src/pages',
  'src/pages/login',
  'src/pages/register',
  'src/store',
  'src/types',
  'src/utils',
  'static'
]

requiredDirs.forEach(dir => {
  const dirPath = path.join(process.cwd(), dir)
  if (fs.existsSync(dirPath)) {
    console.log(`  ✅ ${dir}/`)
  } else {
    console.log(`  ❌ ${dir}/ - 目录不存在`)
    allFilesExist = false
  }
})

console.log('\n📦 检查依赖配置:')
try {
  const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))

  const requiredDeps = [
    'vue',
    '@dcloudio/uni-app',
    '@dcloudio/vite-plugin-uni'
  ]

  requiredDeps.forEach(dep => {
    if (packageJson.dependencies && packageJson.dependencies[dep]) {
      console.log(`  ✅ ${dep}: ${packageJson.dependencies[dep]}`)
    } else if (packageJson.devDependencies && packageJson.devDependencies[dep]) {
      console.log(`  ✅ ${dep}: ${packageJson.devDependencies[dep]} (dev)`)
    } else {
      console.log(`  ❌ ${dep} - 依赖缺失`)
      allFilesExist = false
    }
  })
} catch (error) {
  console.log('  ❌ 无法读取 package.json')
  allFilesExist = false
}

console.log('\n🎯 测试结果:')
if (allFilesExist) {
  console.log('  ✅ 框架结构完整，可以开始开发！')
  console.log('\n🚀 下一步:')
  console.log('  1. 运行: pnpm install (或 npm install)')
  console.log('  2. 使用 HBuilderX 打开项目')
  console.log('  3. 选择运行平台开始开发')
  console.log('  4. 修改 src/config/index.ts 中的 API 地址')
} else {
  console.log('  ❌ 框架结构不完整，请检查缺失的文件')
}

console.log('\n📖 更多信息请查看 README.md 和 USAGE.md 文件')
